/**
 * 
 */
package ru.amse.stroganova.ui.command;

import java.util.HashSet;
import java.util.Set;

import ru.amse.stroganova.presentation.EdgePresentation;
import ru.amse.stroganova.presentation.GraphPresentation;
import ru.amse.stroganova.presentation.VertexPresentation;
import ru.amse.stroganova.ui.visual.GraphElementSelection;

class DeleteCommand implements Command {

	private final GraphPresentation graphPresentation;
	
	private final GraphElementSelection graphSelection;

	private final Set<VertexPresentation> deletedVertices;
	
	private final Set<EdgePresentation> deletedEdges;
	
	/**
	 * Creates new DeleteCommand.
	 * 
	 * @param graphPresentation 
	 * @param graphSelection 
	 */
	public DeleteCommand(GraphPresentation graphPresentation, GraphElementSelection graphSelection) {
		this.graphSelection = graphSelection;
		this.graphPresentation = graphPresentation;
		deletedVertices = new HashSet<VertexPresentation>(graphSelection.getSelectedVertices());
		deletedEdges = new HashSet<EdgePresentation>(graphSelection.getSelectedEdges());
		for (VertexPresentation vp : deletedVertices) {
			deletedEdges.addAll(graphPresentation.getIncidendEdgePresentations(vp));
		}
	}

	public void execute() {
		graphSelection.unselect();
		
		graphPresentation.pauseFiringChanges();
		for (EdgePresentation ep : deletedEdges) {
			graphPresentation.removeEdge(ep);
		}
		for (VertexPresentation vp : deletedVertices) {
			graphPresentation.removeVertex(vp);
		}
		
		graphPresentation.resumeFiringChanges();
	}

	public void undo() {
		graphSelection.unselect();
		
		graphPresentation.pauseFiringChanges();
		for (VertexPresentation vp : deletedVertices) {
			graphPresentation.addVertex(vp);
			graphSelection.selectVertex(vp);
		}
		for (EdgePresentation ep : deletedEdges) {
			graphPresentation.addEdge(ep);
			graphSelection.selectEdge(ep);
		}
		graphPresentation.resumeFiringChanges();
	}

}